<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('产品信息列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <li>
                                <label>[[#{productName}]]：</label>
                                <input type="text" name="productName"/>
                            </li>
                            <li>
                                <label>[[#{sku}]]：</label>
                                <input type="text" name="productCode"/>
                            </li>
                            <li>
                                <label>[[#{spu}]]：</label>
                                <input type="text" name="styleNo"/>
                            </li>
                            <li class="col-sm-3">
                                <label>[[#{product.category}]]：</label>
                                <div class="input-group">
                                    <input name="categoryId" class="form-control" type="hidden">
                                    <input id="suggest-category" class="form-control" type="text">
                                    <div class="input-group-btn">
                                        <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                                            <span class="caret"></span>
                                        </button>
                                        <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                        </ul>
                                    </div>
                                </div>
                            </li>
                            <li>
                                <label>[[#{attributes}]]：</label>
                                <select name="attributes" th:with="type=${@dict.getType('jt_product_attributes')}">
                                    <option value="">[[#{all}]]</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <label>[[#{placeOfProduction}]]：</label>
                                <input type="text" name="countryOfOrigin"/>
                            </li>
                            <li>
                                <label>[[#{status}]]：</label>
                                <select name="status" th:with="type=${@dict.getType('sys_normal_disable')}">
                                    <option value="">[[#{all}]]</option>
                                    <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;[[#{search}]]</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;[[#{reset}]]</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">

            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-suggest-js" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <script th:inline="javascript">
        var attributesDatas = [[${@dict.getType('jt_product_attributes')}]];
        var statusDatas = [[${@dict.getType('sys_normal_disable')}]];
        var prefix = ctx + "business/product";

        $(function() {
            var options = {
                url: prefix + "/list",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: i18n('productInfo'),
                clickToSelect:true,
                columns: [{
                    checkbox: true
                },
                {
                    field: 'productId',
                    title: '产品id',
                    visible: false
                },
                {
                    field: 'thumbnailUrl',
                    title: i18n('image'),
                    formatter: function(value, row, index) {
                        var imageUrl = value;
                        if(ctx.length > 1 && imageUrl != ""){
                            if(imageUrl.indexOf("/") == 0){
                                imageUrl = ctx + imageUrl.substr(1);
                            } else {
                                imageUrl = ctx + imageUrl;
                            }
                        }
                        return imageLazyLoadView(imageUrl, "auto", 750);
                    }
                },
                {
                    field: 'productName',
                    title: i18n('productName')
                },
                {
                    field: 'productCode',
                    title: i18n('sku')
                },
                {
                    field: 'styleNo',
                    title: i18n('spu')
                },
                {
                    field: 'categoryNameZh',
                    title: i18n('product.category')
                },
                {
                    field: 'attributes',
                    title: i18n('attributes'),
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(attributesDatas, value);
                    }
                },
                {
                    field: 'weight',
                    title: i18n('weight')+'(KG)'
                },
                {
                    field: 'unit',
                    title: i18n('unit')
                },
                {
                    field: 'cost',
                    title: i18n('costPrice')
                },
                {
                    field: 'purchasePrice',
                    title:  i18n('purchasePrice')
                },
                {
                    field: 'spec',
                    title: i18n('spec')
                },
                {
                    field: 'countryOfOrigin',
                    title: i18n('placeOfProduction')
                },
                {
                    field: 'supplierName',
                    title: i18n('supplier')
                },
                {
                    field: 'soldQty',
                    title: i18n('soldQty')
                },
                {
                    field: 'stock',
                    title: i18n('stock')
                },
                {
                    field: 'status',
                    title: i18n('status'),
                    formatter: function(value, row, index) {
                       return $.table.selectDictLabel(statusDatas, value);
                    }
                },
                {
                    field: 'createBy',
                    title: i18n('createBy')
                },
                {
                    field: 'createTime',
                    title: i18n('createTime')
                }]
            };
            $.table.init(options);
        });


        // 图片预览
        function imageLazyLoadView(value, height, width, target) {
            if ($.common.isEmpty(width)) {
                width = 'auto';
            }
            if ($.common.isEmpty(height)) {
                height = 'auto';
            }
            if($.common.isMobile()){
                target = "blank";
            }
            var windowWidth = $(window).width();
            //console.log("imgW====="+windowWidth)
            var imgW = 'img-md';
            if(windowWidth < 1024){
                imgW = 'img-lg';
            }
            // blank or self
            var _target = $.common.isEmpty(target) ? 'self' : target;
            if ($.common.isNotEmpty(value)) {
                return $.common.sprintf("<img class='img-circle "+imgW+" lazyload' data-type='2' data-height='%s' data-width='%s' data-target='%s' src='%s' />", height, width, _target, value);
            } else {
                return $.common.nullToStr(value);
            }
        }
    </script>
    <script th:inline="javascript">
        var categorySuggest = $("#suggest-category").bsSuggest({
            url: ctx + "business/category/select/list",
            idField: "categoryId",
            keyField: "categoryNameZh",
            clearable: true,
            ignorecase: true,
            hideOnSelect: true,
            effectiveFields: ["categoryId", "categoryNameZh", "categoryNameEn", "categoryNameAr"],
            effectiveFieldsAlias: {categoryId: "分类ID", categoryNameZh: "中文", categoryNameEn: "English", categoryNameAr: "arabic"},
        }).on('onSetSelectValue', function (e, selectedData, selectedRawData) {
            console.log('onSetSelectValue: ', selectedData);

            $("input[name='categoryId']").val(selectedRawData.categoryId);
        }).on('onUnsetSelectValue', function (e) {
            //console.log("onUnsetSelectValue=="+e);
            $("input[name='categoryId']").val("");
        });
        
        
        function getSelections() {
            var selectedRow = $("#bootstrap-table").bootstrapTable('getSelections');
            console.log("===select===")
            return selectedRow;
        }
        
    </script>

     <script th:inline="javascript">
         var ROOT = [[${#servletContext.contextPath}]];
         var LANG_COUNTRY = [[${#locale.language+'_'+#locale.country}]];

         //初始化i18n
         $.i18n.properties({
             path: ROOT + '/i18n/',//这里表示访问路径
             name: 'messages',//文件名开头
             language: LANG_COUNTRY,//文件名语言 例如en_US
             mode: 'map'//默认值
         });

         //i18n函数
         function i18n(msgKey) {
             try {
                 return $.i18n.prop(msgKey);
             } catch (e) {
                 return msgKey;
             }
         }
     </script>
</body>
</html>